package nbcp


import org.slf4j.LoggerFactory
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext

import nbcp.base.extend.*
import nbcp.base.utils.SpringUtil
import nbcp.db.*
import nbcp.extend.debugServerInfo
import nbcp.web.HttpContext
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.core.type.AnnotatedTypeMetadata
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor
import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.*
import org.springframework.scheduling.config.TaskManagementConfigUtils
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer
import java.util.*


@EnableDubbo
@SpringBootApplication
@ServletComponentScan
@Import(SpringUtil::class)
@EnableAuthorizationServer
open class MainApplication {
    companion object {
        val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }

    @Bean
    fun webSecurityConfigurerAdapter(): WebSecurityConfigurerAdapter {
        return object : WebSecurityConfigurerAdapter() {
            override fun configure(httpSecurity: HttpSecurity) {
                httpSecurity.formLogin().and().csrf().disable()
            }
        }
    }
}

@Configuration
@EnableScheduling
@ConditionalOnProperty(name = ["shop.scheduler"], havingValue = "true", matchIfMissing = true)
class Scheduler {
}

fun init() {
//    SystemContext.getUserNameFunc = {
//        var request = HttpContext.nullableRequest;
//        if (request == null) "";
//        else request!!.UserName;
//    }
}


fun main(args: Array<String>) {
//    局显示定义最省事安全
//    Locale.setDefault(Locale.ENGLISH);//推荐用英语地区的算法
    System.setProperty("user.timezone", "Asia/Shanghai");
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));

    var context = SpringApplication.run(MainApplication::class.java, *args) as ServletWebServerApplicationContext

    MainApplication.logger.trace("""
        ================================================
             ${context.debugServerInfo}
        ================================================
""")


    init();
}